package com.fanhao.dllo.justforyou.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.activity.FoodHouseInforActivity;
import com.fanhao.dllo.justforyou.entity.FoodHouseListResult;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;

import java.util.List;

public class FoodHouseAdapter extends RecyclerView.Adapter<
        FoodHouseAdapter.FoodHouseViewHolder> {
    private List<FoodHouseListResult.DataEntity.Poi_list> poi_list;

    private Context context;



    public FoodHouseAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<FoodHouseListResult.DataEntity.Poi_list> datas){
        poi_list = datas;
        notifyDataSetChanged();
    }

    /**缓存组件初始化***/
    public class FoodHouseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private LinearLayout linearLayout;
        private NetworkImageView imgIv;
        private TextView nameTv,bizarea_title,short_title,sale_count;
        private int position;
        private String id;

        public FoodHouseViewHolder(View v) {
            super(v);
            imgIv = (NetworkImageView) v.findViewById(R.id.imgIv);
            nameTv = (TextView)v.findViewById(R.id.nameTv);
//            bizarea_title = (TextView)v.findViewById(R.id.bizarea_title);
            short_title = (TextView)v.findViewById(R.id.short_title);
            sale_count = (TextView)v.findViewById(R.id.sale_count);
            linearLayout = (LinearLayout)v.findViewById(R.id.linearLayout);

            linearLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, FoodHouseInforActivity.class);
            intent.putExtra("id",id);
            Log.d("FoodHouseViewHolder", id);
            context.startActivity(intent);
        }
    }

    @Override
    public FoodHouseViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        LayoutInflater inflater = LayoutInflater.from(
                parent.getContext());

        View v = inflater.inflate(R.layout.
                food_house__list_item,null);

        return new FoodHouseViewHolder(v);
    }

    @Override
    public void onBindViewHolder(FoodHouseViewHolder holder, int position) {


        if (poi_list != null) {
            FoodHouseListResult.DataEntity.Poi_list poi = poi_list.get(position);
            holder.nameTv.setText(poi.getPoi_name());
//            holder.bizarea_title.setText(poi.getBizarea_title());

            holder.id = poi.getPoi_id();

            List<FoodHouseListResult.DataEntity.Poi_list.Tuan_list> tuan_list = poi.getTuan_list();
            holder.short_title.setText(tuan_list.get(0).getShort_title());
            holder.sale_count.setText(tuan_list.get(0).getSale_count()+"");

            String url = poi.getPoi_image();
            getBitmap(url,holder.imgIv);

          holder.position = position;

        }
    }

    private void getBitmap(String url, NetworkImageView imageView) {
        ImageLoader imageLoader = VolleySingleton.getInstance().getImageLoader();
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url,imageLoader);
    }

    @Override
    public int getItemCount() {
        return poi_list != null && poi_list.size() > 0 ? poi_list.size() : 0;
    }



}
